package questions;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.PriorityQueue;

public class Questions {
	
	private File progressFile;
	private File questionsFile;
	private PriorityQueue<Entry> queue;
		
	public Questions()
	{
		queue = new PriorityQueue<Entry>();
		progressFile = new File(resource.Resource.getProgressPath());
		questionsFile = new File(resource.Resource.getQuestionsFilePath());
	}
	
	public boolean progressExists(){
		return progressFile.exists();
	}
	public boolean questionsExists(){
		return questionsFile.exists();
	}
	
	public void load() throws FileNotFoundException, IOException{
		if(!progressExists())
			if(!questionsExists())
				throw new FileNotFoundException("il file questions.txt non e' stato trovato!");
			else
				pickQuestions();
		else
			pickProgress();
	}
	public void pickQuestions() throws IOException
	{
		ArrayList<Entry> arr = new ArrayList<Entry>();
		
		BufferedReader in = new BufferedReader(new FileReader(questionsFile));
		String line = in.readLine();
		String section;
		
		if(line != null && !line.equals(""))
			section = line;
		else {
			in.close();
			throw new IOException("il file questions.txt ha una sintassi sbagliata");
		}
		in.readLine();
		line = in.readLine();
		
		while(line != null)
		{
			if(line.equals(""))
			{
				section = in.readLine();
				in.readLine();
				line = in.readLine();
			}
			else
			{
				arr.add(new Entry(section, line));
				line = in.readLine();
			}
		}
		in.close();
		Collections.shuffle(arr);
		queue = new PriorityQueue<Entry>(arr);
	}
	
	public void saveProgress() throws IOException
	{
		FileWriter file = new FileWriter(progressFile, true);
		PrintWriter out = new PrintWriter(new BufferedWriter(file));
		for(Entry elem: queue){
			out.println(encodeEntry(elem));
		}
		queue.clear();
		
//		Entry now;
//		while(! queue.isEmpty())
//		{
//			
//			now = queue.poll();
//			String str = now.getSection()+"%"+now.getType()+"%"+now.getQuestion()+"%"+now.getPage()+"%"+now.getPriority()+"%"+now.getDate();
//			out.println(str);
//		}
		out.close();
	
	}
	
	public void pickProgress() throws IOException
	{
		BufferedReader in = new BufferedReader(new FileReader(progressFile));
		String line = in.readLine();
		
		while(line != null)
		{
			String[] parsed = line.split("%");
			Entry one = new Entry(parsed[0],parsed[1], parsed[2],Integer.parseInt(parsed[3]),Integer.parseInt(parsed[4]),Long.parseLong(parsed[5]));
			queue.add(one);
			line = in.readLine();
		}
		in.close();
		
		progressFile.renameTo(new File("progress.bak"));	
	}
	
	public void selectOnlyDim() throws IOException
	{
		FileWriter file = new FileWriter(progressFile, true);
		PrintWriter out = new PrintWriter(new BufferedWriter(file));
				
		PriorityQueue<Entry> dimQueue = new PriorityQueue<Entry>();
		
		for(Entry elem: queue){
			if(elem.getType().contains("dim") || elem.getType().contains("Dim") || elem.getType().contains("DIM")){
				dimQueue.add(elem);
			}else{
				out.println(encodeEntry(elem));
			}
		}
		
//		Entry now;
//		while( !queue.isEmpty() )
//		{
//			
//			now = queue.poll();
//			if(now.getType().contains("dim") || now.getType().contains("Dim") || now.getType().contains("DIM"))
//				dimQueue.add(now);
//			else
//			{
//				String str = now.getSection()+"%"+now.getType()+"%"+now.getQuestion()+"%"+now.getPage()+"%"+now.getPriority()+"%"+now.getDate();
//				out.println(str);
//			}
//		}
		
		out.close();
		queue = dimQueue;
	}
	
	public Entry getEntry(){
		if(queue.isEmpty())
			throw new RuntimeException("pila vuota");
		return queue.peek();
	}
	
	public void reinsertEntry(Entry e){
		queue.poll();
		queue.add(e);
	}
	
	public void deleteProgress(){
		progressFile.delete();
	}
	
	public void print()	{
		
		for(Entry e: queue){
			String str = encodeEntry(e);
			System.out.println(str);
		}
	}
	private String encodeEntry(Entry e){
		return ( e.getSection()+"%"+e.getType()+"%"+e.getQuestion()+"%"+e.getPage()+"%"+e.getPriority()+"%"+e.getDate());
	}
}
